<?
function startElement($parser, $name, $attrs) {
    global $stack;
    global $base;
    global $counter;
    $stack[] = $name;

    $array_ref =& $base[$stack[0]];

    $counter_array_ref =& $counter[$stack[0]];
    for($i = 1; $i < sizeof($stack); $i++) {
        $counter_array_ref =& $counter_array_ref[$stack[$i]];
    }
    $counter_array_ref[count]++;

    $counter_array_ref =& $counter[$stack[0]];
    for($i = 1; $i < sizeof($stack); $i++) {
        $counter_array_ref =& $counter_array_ref[$stack[$i]];
            if ($stack[$i-1] != 'CONTRIB') {
                $array_ref =& $array_ref[($counter_array_ref['count']-1)];
            }
        $array_ref =& $array_ref[$stack[$i]];
    }
    $array_ref['@'] = $attrs;
/*
    global $function;
    global $curr_element;
    switch ($name) {
        case 'FINDREPLACE':
            $function = 'FINDREPLACE';
            break;
        case 'ADDCODE':
            $function = 'ADDCODE';
            break;
        case 'ADDFILE':
            $function = 'ADDFILE';
            break;
        case 'SQL':
            $function = 'SQL';
            break;
    }
    $curr_element = $function.'_'.$name;

    switch ($name) {
        case 'FILE':
        case 'ORIGINALLINENUMBERS':
        case 'FINDLINENUMBERS':
            $curr_element_attributes = $curr_element . '_attr';
            global $$curr_element_attributes;
            ${$curr_element_attributes}[] = $attrs;
            echo $curr_element_attributes . "\n";
            print_r($$curr_element_attributes);
            break;
    }
*/
}

function endElement($parser, $name) {
    global $stack;
    array_pop($stack);
}

function dataHandler($parser, $data) {
    global $stack;
    global $base;
    global $counter;

    $array_ref =& $base[$stack[0]];
    $counter_array_ref =& $counter[$stack[0]];
    $data = ltrim($data);
    if ($data != '') {
        for($i = 1; $i < sizeof($stack); $i++) {
//          $array_ref =& $array_ref[];
            $counter_array_ref =& $counter_array_ref[$stack[$i]];
            if ($stack[$i-1] != 'CONTRIB') {
                $array_ref =& $array_ref[($counter_array_ref['count']-1)];
            }
            $array_ref =& $array_ref[$stack[$i]];
        }
        $array_ref['#'] = $data;
    }
/*
    global $curr_element;
    global $$curr_element;
    $data = ltrim($data);
    if ($data <> '')
        ${$curr_element}[] = $data;
//  echo $curr_element . "\n" . $data . "\n\n";
*/
}

function parse($file) {
    global $base;
    global $counter;
    $base = array();
    $counter = array();

    //$xml_parser = xml_parser_create();
    $xml_parser = xml_parser_create('UTF-8');//Vlad added this
    xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 1);
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "dataHandler");
    if (!(@ $fp = fopen("$file", "r")))
        error (ERROR_COULD_NOT_OPEN_XML.$file, '');
    while ($data = fread($fp, 4096))
        $file_contents .= $data;
    if(!xml_parse($xml_parser, $file_contents, 1)) {
        die(sprintf(ERROR_XML."%s".TEXT_AT_LINE."%d\n", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
    }
    xml_parser_free($xml_parser);
    return $base;
}

function array_path_insert(&$array, $path, $value) {
    $arr_ref =& $array;

    for($i = 0; $i < sizeof($path); $i++) {
        $arr_ref =& $arr_ref[$path[$i]];
    }

    $arr_ref = $value;
}

/*  Test print
parse('install.xml');
print_r($base);
*/
?>
